package com.snail.billing.page;

import java.util.Timer;
import java.util.TimerTask;

import android.content.res.ColorStateList;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.net.HttpApp;
import com.android.net.HttpResult;
import com.android.net.HttpSession;
import com.android.net.OnHttpResultListener;
import com.android.util.AlertUtil;
import com.android.util.ResUtil;
import com.snail.billing.Resource;
import com.snail.billing.json.JsonBase;
import com.snail.billing.os.DialogPage;
import com.snail.billing.session.ForgetPwdResetEmailSession;
import com.snail.billing.session.ForgetPwdResetMobileSession;
import com.snail.billing.session.ForgetPwdSendEmailSession;
import com.snail.billing.session.ForgetPwdSendMoblieSession;
import com.snail.billing.util.AccountCaptcha;

public class ForgetPwdPage extends DialogPage implements View.OnClickListener, OnHttpResultListener {

	private TextView textTitle;
	private View buttonBack;

	private TextView textAccount;
	private EditText inputPwd;
	private EditText inputPwd2;
	private EditText inputCaptcha;
	private Button buttonSend;
	private View button;

	private HttpApp httpApp;
	private ForgetPwdSendMoblieSession forgetPwdSendMoblieSession;
	private ForgetPwdSendEmailSession forgetPwdSendEmailSession;
	private ForgetPwdResetMobileSession forgetPwdResetMobileSession;
	private ForgetPwdResetEmailSession forgetPwdResetEmailSession;

	private int state;
	private String accountString;
	private String pwdString;
	private String captchaString;

	private Timer timer;
	private TimerTask task;
	private Handler handler;
	private Drawable buttonSendBackground;
	private ColorStateList buttonSendColorStateList;
	private String buttonSendText;

	private class SendSleepTask extends TimerTask {

		int i = 60;

		@Override
		public void run() {
			i--;
			if (i > 0) {
				handler.post(new Runnable() {

					@Override
					public void run() {
						buttonSend.setClickable(false);
						buttonSend.setBackgroundResource(ResUtil.getDrawableId(Resource.drawable.snailbilling_button_bg_gray));
						buttonSend.setTextColor(0xffffffff);
						buttonSend.setText(buttonSendText + "(" + i + ")");
					}
				});
			} else {
				handler.post(new Runnable() {

					@Override
					public void run() {
						buttonSend.setClickable(true);
						buttonSend.setBackgroundDrawable(buttonSendBackground);
						buttonSend.setTextColor(buttonSendColorStateList);
						buttonSend.setText(buttonSendText);
					}
				});
			}
		}
	};

	@Override
	public int createLayoutId() {
		return ResUtil.getLayoutId(Resource.layout.snailbilling_forget_pwd_activity);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		textTitle = (TextView) findViewById(ResUtil.getViewId(Resource.id.snailbilling_title_text));
		textTitle.setText(ResUtil.getString(Resource.string.snailbilling_forget_title));
		buttonBack = findViewById(ResUtil.getViewId(Resource.id.snailbilling_title_button_back));
		buttonBack.setOnClickListener(this);
		//
		state = getPageArgs().getInt("state");
		accountString = getPageArgs().getString("account");
		//
		textAccount = (TextView) findViewById(ResUtil.getViewId(Resource.id.snailbilling_forget_pwd_text_account));
		textAccount.setText(accountString);
		inputPwd = (EditText) findViewById(ResUtil.getViewId(Resource.id.snailbilling_forget_pwd_input_pwd));
		inputPwd2 = (EditText) findViewById(ResUtil.getViewId(Resource.id.snailbilling_forget_pwd_input_pwd2));
		inputCaptcha = (EditText) findViewById(ResUtil.getViewId(Resource.id.snailbilling_forget_pwd_input_captcha));
		buttonSend = (Button) findViewById(ResUtil.getViewId(Resource.id.snailbilling_forget_pwd_button_send));
		button = findViewById(ResUtil.getViewId(Resource.id.snailbilling_forget_pwd_button));
		buttonSend.setOnClickListener(this);
		button.setOnClickListener(this);
		//
		buttonSendBackground = buttonSend.getBackground();
		buttonSendColorStateList = buttonSend.getTextColors();
		buttonSendText = buttonSend.getText().toString();
		//
		httpApp = new HttpApp(getContext());
		httpApp.setOnHttpResultListener(this);
		//
		handler = new Handler();
		task = new SendSleepTask();
		timer = new Timer();
		timer.schedule(task, 0, 1000);
	}

	@Override
	public void onClick(View v) {
		if (v.equals(buttonBack)) {
			if (timer != null) {
				timer.cancel();
			}
			getPageManager().backward();
		} else if (v.equals(buttonSend)) {
			if (timer != null) {
				timer.cancel();
			}
			task = new SendSleepTask();
			timer = new Timer();
			timer.schedule(task, 0, 1000);
			if (state == 0) {
				forgetPwdSendMoblieSession = new ForgetPwdSendMoblieSession(accountString);
				httpApp.request(forgetPwdSendMoblieSession);
			} else if (state == 1) {
				forgetPwdSendEmailSession = new ForgetPwdSendEmailSession(accountString);
				httpApp.request(forgetPwdSendEmailSession);
			}
		} else if (v.equals(button)) {
			pwdString = inputPwd.getText().toString();
			if (TextUtils.isEmpty(pwdString)) {
				AlertUtil.show(getContext(), ResUtil.getString(Resource.string.snailbilling_forget_pwd_input_pwd));
				return;
			}
			String pwdString2 = inputPwd2.getText().toString();
			if (TextUtils.isEmpty(pwdString2)) {
				AlertUtil.show(getContext(), ResUtil.getString(Resource.string.snailbilling_forget_pwd_input_pwd2));
				return;
			}
			captchaString = inputCaptcha.getText().toString();
			if (TextUtils.isEmpty(captchaString)) {
				AlertUtil.show(getContext(), ResUtil.getString(Resource.string.snailbilling_forget_pwd_input_captcha));
				return;
			}
			if (!AccountCaptcha.validatePwd(getContext(), accountString, pwdString, pwdString2)) {
				return;
			}
			if (state == 0) {
				forgetPwdResetMobileSession = new ForgetPwdResetMobileSession(accountString, captchaString, pwdString);
				httpApp.request(forgetPwdResetMobileSession);
			} else if (state == 1) {
				forgetPwdResetEmailSession = new ForgetPwdResetEmailSession(accountString, captchaString, pwdString);
				httpApp.request(forgetPwdResetEmailSession);
			}
		}
	}

	@Override
	public void onHttpResult(HttpResult httpResult) {
		if (httpResult.isSucceed()) {
			HttpSession httpSession = httpResult.getHttpSession();
			if (httpSession.equals(forgetPwdSendMoblieSession)) {
				JsonBase jsonBase = new JsonBase((String) httpSession.getResponseData());
				Toast.makeText(getContext(), jsonBase.getMessage(), Toast.LENGTH_SHORT).show();
			} else if (httpSession.equals(forgetPwdSendEmailSession)) {
				JsonBase jsonBase = new JsonBase((String) httpSession.getResponseData());
				Toast.makeText(getContext(), jsonBase.getMessage(), Toast.LENGTH_SHORT).show();
			} else if (httpSession.equals(forgetPwdResetMobileSession)) {
				JsonBase jsonBase = new JsonBase((String) httpSession.getResponseData());
				Toast.makeText(getContext(), jsonBase.getMessage(), Toast.LENGTH_SHORT).show();
				if (jsonBase.getCode() == 1) {
					if (timer != null) {
						timer.cancel();
					}
					getPageManager().backward(1);
				}
			} else if (httpSession.equals(forgetPwdResetEmailSession)) {
				JsonBase jsonBase = new JsonBase((String) httpSession.getResponseData());
				Toast.makeText(getContext(), jsonBase.getMessage(), Toast.LENGTH_SHORT).show();
				if (jsonBase.getCode() == 1) {
					if (timer != null) {
						timer.cancel();
					}
					getPageManager().backward(1);
				}
			}
		}
	}

}
